package com.wlxk.base.common.dto;

import com.wlxk.base.common.constant.WlErrEnum;
import com.wlxk.base.common.exception.WlException;
import com.wlxk.base.common.query.WlQuery;

import java.io.Serializable;

/**
 * @author wlxk
 * @since 2021-11-03 17:07:39
 */
public class WlResult<T> implements Serializable {

    private static final long serialVersionUID = 1596887802775651209L;

    /**
     * 成功code
     */
    public static final int SUCCESS = 200;
    /**
     * 通用失败code
     */
    public static final int FAIL = 100501;
    /**
     * 失败消息
     */
    private String msg;
    /**
     * 错误码
     */
    private int code;
    /**
     * 成功标识
     */
    private boolean success;
    /**
     * 返回实体
     */
    private T data;
    /**
     * 查询参数
     */
    private WlQuery query;


    public WlResult(int code, String msg, T data, boolean success, WlQuery query) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = success;
        this.query = query;
    }

    public static <T> WlResult<T> fail(String msg) {
        return fail(FAIL, msg);
    }

    public static <T> WlResult<T> fail(WlException e) {
        return fail(e.getCode(), e.getMessage());
    }

    public static <T> WlResult<T> fail(WlErrEnum e) {
        return fail(e.code, e.msg);
    }

    public static <T> WlResult<T> fail(Integer code, String msg) {
        return new WlResult<T>(code, msg, null, false, null);
    }

    public static <T> WlResult<T> ok() {
        return ok(null);
    }

    public static <T> WlResult<T> ok(T data) {
        return new WlResult<T>(SUCCESS, null, data, true, null);
    }

    public static <T> WlResult<T> ok(T data, WlQuery query) {
        return new WlResult<T>(SUCCESS, null, data, true, query);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public WlQuery getQuery() {
        return query;
    }

    public void setQuery(WlQuery query) {
        this.query = query;
    }
}
